<?php
namespace app\admin\controller;
use app\admin\logic\WorkStationMessageLogic;

/**
 * 工位留言api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2020/04/10
 * Time: 16:48
 */
class WorkStationMessage extends Base {

    public function __construct(){
        parent::__construct();
        $this->wsm_logic = new WorkStationMessageLogic();
    }

    /**
     * 获取工位留言
     * @return \think\response\Json
     */
    public function ajax_get_wsm(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->wsm_logic->get_search_wsm_maps($params['openid'],$params['branchid'],$params['floorid'],$params['regionid'],$params['number'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('work_station_message')->get_list($maps,'id,openid,wsid,message,enable,c_time','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            //获取微信用户信息
            $v_maps = [];
            $v_maps[] = ['openid','=',$v['openid']];
            $v_detail = model('visitor')->get_detail($v_maps,'id,nickname');
            $list[$k]['nickname'] = !empty($v_detail) ? $v_detail['nickname'] : '';

            //获取用户信息
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,tel');
            $list[$k]['tel'] = !empty($u_detail) ? $u_detail['tel'] : '';

            //获取工位
            $ws_maps = [];
            $ws_maps[] = ['id','=',$v['wsid']];
            $ws_detail = model('work_station')->get_detail($ws_maps,'id,regionid,number');
            $list[$k]['number'] = !empty($ws_detail) ? $ws_detail['number'] : '';

            $r_maps = [];
            $r_maps[] = ['id','=',$ws_detail['regionid']];
            $r_detail = model('region')->get_detail($r_maps,'id,floorid');

            //获取楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$r_detail['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
            $list[$k]['floor_zw'] = !empty($f_detail) ? $f_detail['name'] : '';

            //获取分部
            $b_maps = [];
            $b_maps[] = ['id','=',$f_detail['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $list[$k]['branch_zw'] = !empty($b_detail) ? $b_detail['name'] : '';
        }

        $total_num = model('work_station_message')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->wsm_logic->get_response(1,'OK',$data));
    }

    /**
     * 启用工位留言
     * @return \think\response\Json
     */
    public function ajax_on_wsm(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['id', '=', $params['wsmid']];

        $wsm_data = [];
        $wsm_data['enable'] = 1;
        $r = model('work_station_message')->force()->save($wsm_data,$maps);
        if($r !== false){
            return json($this->wsm_logic->get_response(1, '修改成功'));
        }
        return json($this->wsm_logic->get_response(0, '修改失败'));
    }

    /**
     * 禁用工位留言
     * @return \think\response\Json
     */
    public function ajax_off_wsm(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['id', '=', $params['wsmid']];

        $wsm_data = [];
        $wsm_data['enable'] = 0;
        $r = model('work_station_message')->force()->save($wsm_data,$maps);
        if($r !== false){
            return json($this->wsm_logic->get_response(1, '修改成功'));
        }
        return json($this->wsm_logic->get_response(0, '修改失败'));
    }
}